<template>
	<view class="wrap">
		<u-waterfall v-model="flowList" ref="uWaterfall">
			<template v-slot:left="{leftList}">
				<view class="demo-warter" v-for="(item, index) in leftList" :key="index"
					@click="toRecipeDetail(item.recipe_code)">
					<!-- 警告：微信小程序中需要hx2.8.11版本才支持在template中结合其他组件，比如下方的lazy-load组件 -->
					<u-lazy-load threshold="150" border-radius="10" :image="item.logo_img_url_s" :index="index">
					</u-lazy-load>
					<view class="demo-title">
						{{item.title}}
					</view>
				</view>
			</template>
			<template v-slot:right="{rightList}">
				<view class="demo-warter" v-for="(item, index) in rightList" :key="index"
					@click="toRecipeDetail(item.recipe_code)">
					<u-lazy-load threshold="150" border-radius="10" :image="item.logo_img_url_s" :index="index">
					</u-lazy-load>
					<view class="demo-title">
						{{item.title}}
					</view>
				</view>
			</template>
		</u-waterfall>
		<u-loadmore bg-color="rgb(240, 240, 240)" :status="loadStatus" @loadmore="addRandomData"></u-loadmore>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				page: 1,
				loadStatus: '',
				flowList: [],
				list: []
			}
		},
		onLoad() {
			this.addRandomData();
		},
		onPullDownRefresh: function() {
			this.page=1;
			this.flowList=[];
			this.addRandomData();
		},
		onReachBottom() {
			if(this.loadStatus=='loading'||this.loadStatus=='nomore'){
				return;
			}
			this.addRandomData();
		},
		methods: {
			addRandomData() {
				this.loadStatus = "loading";
				uniCloud.callFunction({
						name: 'recipe',
						data: {
							action: 'pageQueryRecipe',
							page: this.page,
						}
					}).then(res=>{
						console.log(res.result)
						if (res.success) {
							if (res.result.hasMore) {
								this.page = this.page + 1
								this.loadStatus = "loadmore";
							} else {
								this.loadStatus = "nomore";
							}
							this.flowList=this.flowList.concat(res.result.list);
						
						}
						uni.stopPullDownRefresh();
					});
			},
			toRecipeDetail(recipe_code) {
				uni.navigateTo({
					url: '/pages/recipe/detail?recipe_code=' + recipe_code,
					success(res) {
						console.log(res);
					},
					fail(err) {
						console.log(err);
					}
				});
			}
		}
	}
</script>

<style>
	/* page不能写带scope的style标签中，否则无效 */
	page {
		background-color: rgb(240, 240, 240);
	}
</style>

<style lang="scss" scoped>
	.demo-warter {
		border-radius: 8px;
		margin: 5px;
		background-color: #ffffff;
		padding: 8px;
		position: relative;
	}

	.u-close {
		position: absolute;
		top: 32rpx;
		right: 32rpx;
	}

	.demo-image {
		width: 100%;
		border-radius: 4px;
	}

	.demo-title {
		font-size: 30rpx;
		margin-top: 5px;
		color: $u-main-color;
	}

	.demo-tag {
		display: flex;
		margin-top: 5px;
	}

	.demo-tag-owner {
		background-color: $u-type-error;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		padding: 4rpx 14rpx;
		border-radius: 50rpx;
		font-size: 20rpx;
		line-height: 1;
	}

	.demo-tag-text {
		border: 1px solid $u-type-primary;
		color: $u-type-primary;
		margin-left: 10px;
		border-radius: 50rpx;
		line-height: 1;
		padding: 4rpx 14rpx;
		display: flex;
		align-items: center;
		border-radius: 50rpx;
		font-size: 20rpx;
	}

	.demo-price {
		font-size: 30rpx;
		color: $u-type-error;
		margin-top: 5px;
	}

	.demo-shop {
		font-size: 22rpx;
		color: $u-tips-color;
		margin-top: 5px;
	}
</style>
